import 'package:flutter/material.dart';
import '../data/user_product_repository.dart';
import '../entity/game.dart';

class LibraryProvider extends ChangeNotifier {
  final UserProductRepository _libraryRepository = UserProductRepository();

  List<Game> _libraryGames = [];
  List<Game> get libraryGames => _libraryGames;

  bool _isLoading = false;
  bool get isLoading => _isLoading;

  String? _errorMessage;
  String? get errorMessage => _errorMessage;

  //根据用户ID获取游戏库列表
  Future<void> fetchLibrary(String userId) async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    try {
      _libraryGames = await _libraryRepository.getLibraryGamesForUser(userId);
    } catch (e) {
      _errorMessage = "加载游戏库失败: $e";
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }
}